import java.io.*;
import java.net.*;
import java.util.*;
import java.security.Security;
import java.security.Provider;
import javax.net.ssl.*;
import local.util.MyPrint;
import java.text.DecimalFormat;

import local.util.MyStringFormat;
import local.util.MyPrint;

public class IndustryReport {    

    private Vector industryRecordVector = new Vector ();
    DecimalFormat td = new DecimalFormat("0.00");
    DecimalFormat td1 = new DecimalFormat("0");
    MyStringFormat mf = new MyStringFormat();
    int totalStock = 0;
    double totalVolume = 0;
    double totalMoney = 0.0;

    public static void main (String args[]) {

	IndustryReport sreport1 = new IndustryReport ();
	IndustryReport sreport2 = new IndustryReport ();

	Vector t_head_v1   = new Vector();
	Vector t_record_v1 = new Vector();

	Vector t_head_v2   = new Vector();
	Vector t_record_v2 = new Vector();

	String my_query_string = null;
	
	my_query_string = "SELECT * FROM sector_record WHERE SID = '0' ORDER BY DATE";

	DataBaseQuery my_query1 = new DataBaseQuery ();

	my_query1.setQueryString (my_query_string);
	my_query1.lookup (my_query_string);

	t_head_v1    = my_query1.getTableHeadVector();
	t_record_v1  = my_query1.getTableRecordVector();

	//MyPrint.printVector(t_record_v1,1);
	//sreport1.sumDailyTotal(sreport1.getIndustryRecord());
	
	DataBaseQuery my_query2 = new DataBaseQuery ();
	my_query_string ="SELECT * FROM industry_record ORDER BY SID, IID, DATE";

	my_query2.setQueryString (my_query_string);
	my_query2.lookup (my_query_string);

	t_head_v2    = my_query2.getTableHeadVector();
	t_record_v2  = my_query2.getTableRecordVector();

	sreport2.setIndustryRecord(t_record_v2);
	sreport2.printIndustryReport (sreport2.getIndustryRecord());
       
    }

    private void printHelpMessage () {

	System.out.println("========================================");
	System.out.println("Generate Industry Report using db data");
	System.out.println("========================================");
	System.exit(0);
    }

    public void setIndustryRecord (Vector d_v_v) {

	Vector temp_v = new Vector();

	Enumeration enum = d_v_v.elements();
	while ( enum.hasMoreElements()) {
	    temp_v =(Vector)enum.nextElement();
	    IndustryRecord s_r = new IndustryRecord ();
	    s_r.setIndustryRecord(temp_v);
	    industryRecordVector.add(s_r);
	}
    }      

    public void setIndustryTotal (IndustryRecord record) {

	totalStock  = record.getTotalStock();
	totalVolume = record.getTotalVolume();
	totalMoney  = record.getTotalMoney();
	//System.out.println(totalStock);
	//System.out.println(totalVolume);
	//System.out.println(totalMoney);
    }      

    
    public Vector getIndustryRecord ( ) {

	return industryRecordVector;
    }      

    public void sumDailyTotal( Vector input_r_v) {

	double money                = 0.0;
	double money_up             = 0.0;
	double money_down           = 0.0;

	String date1                 = null;
	String date2                 = null;
	String temp1                  = null;
	String temp2                  = null;

	IndustryRecord s_r1 = new IndustryRecord ();
	IndustryRecord s_r2 = new IndustryRecord ();

	Enumeration enum1 = input_r_v.elements();

	while ( enum1.hasMoreElements()) { 

	    s_r1  = (IndustryRecord)enum1.nextElement();
	    date1 = s_r1.getDate();

	    if (!date1.equals(temp1)) {
		Enumeration enum2 = input_r_v.elements();
		while ( enum2.hasMoreElements()) { 
		    s_r2 = (IndustryRecord)enum2.nextElement();

		    date2 = s_r2.getDate();

		    if (date1.equals(date2)) {
			money           += s_r2.getTotalMoney();
			money_up        += s_r2.getTotalMoneyUp();
			money_down      += s_r2.getTotalMoneyDown();
		    }
		    else {
			continue;
		    }
		}

		MyPrint.printLine("=",100);
		System.out.print(mf.leftIndent(date1.replaceAll("00:00:00",""),10,' '));
		System.out.print(mf.rightIndent(td.format(money), 17, ' ')+" ");
		System.out.print(mf.rightIndent(td.format(100*money_up/money),8,' ')+"% ");
		System.out.println(mf.rightIndent(td.format(100*money_down/money),8,' ')+"%");
		MyPrint.printLine("=",100);
		temp1 = date1;

		money           = 0.0;
		money_up        = 0.0;
		money_down      = 0.0;
	    }
	    else {
		continue;
	    }
	}
    }

  public void printIndustryReport( Vector input_r_v) {

	int stock             = 0;
	int stock_up          = 0;
	int stock_down        = 0;

	double volume                 = 0;
	double volume_up              = 0;
	double volume_down            = 0;

	double money                = 0.0;
	double money_up             = 0.0;
	double money_down           = 0.0;

	double net_money           = 0.0;

	double industry_weight        = 0.0;

	String date                 = null;
	String sector_id            = null;
	String sector_name          = null;
	String industry_id          = null;
	String industry_name        = null;
	String temp                 = null;
	IndustryRecord s_r = new IndustryRecord ();

	NasdaqReport naz = new NasdaqReport();
	naz.setNazRecordVector("06/18/2002");

	Enumeration enum = input_r_v.elements();

	while ( enum.hasMoreElements()) { 
	    s_r = (IndustryRecord)enum.nextElement();

	    date = s_r.getDate();
	    sector_name     = s_r.getSectorName();
	    sector_id       = s_r.getSectorId();
	    industry_name   = s_r.getIndustryName();
	    industry_id     = s_r.getIndustryId();

	    if (!industry_name.equals(temp)) {
		MyPrint.printLine("=",130);
		System.out.print("Sector Name: "+sector_name+" Sector Id: ");
		System.out.print(sector_id);
		System.out.print("\tIndustry Name: "+industry_name+" Industry Id: ");
		System.out.println(industry_id);
		net_money = 0.0;
		MyPrint.printLine("=",130);
	    }

	    System.out.print(mf.leftIndent(date.replaceAll("00:00:00",""),10,' '));
	    // System.out.print("["+mf.leftIndent(sector_id,4,' ')+"]");
	   
	    stock     = s_r.getTotalStock();
	    stock_up  = s_r.getTotalStockUp();
	    stock_down= s_r.getTotalStockDown();

	    System.out.print  (mf.rightIndent(stock, 6, ' ')+" ");
	    //System.out.print  (mf.rightIndent(total_stock_up, 4, ' ')+" ");
	    System.out.print  (mf.rightIndent(100*stock_up/(stock), 4, ' ')+"% ");

	    //System.out.print  (mf.rightIndent(total_stock_down, 4, ' ')+" ");
	    System.out.print  (mf.rightIndent(100*(stock_down)/(stock), 4, ' ')+"% ");

	    System.out.print(mf.rightIndent(naz.getNazChange(date),8,' '));

	    volume          = s_r.getTotalVolume();
	    volume_up       = s_r.getTotalVolumeUp();
	    volume_down     = s_r.getTotalVolumeDown();

	    System.out.print(mf.rightIndent(td1.format(volume),11,' '));
	    //System.out.print(mf.rightIndent(volume_up, 10, ' ')+" "+"("+
	    System.out.print(mf.rightIndent(td1.format(volume_up*100/volume),5,' ')+"%");
	    //System.out.print(mf.rightIndent(volume_down,10,' ')+" "+"("+
	    System.out.print(mf.rightIndent(td1.format(volume_down*100/volume),5,' ')+"%");
	    System.out.print(mf.rightIndent(naz.getNazChange(date),8,' '));

	    money           = s_r.getTotalMoney();
	    money_up        = s_r.getTotalMoneyUp();
	    money_down      = s_r.getTotalMoneyDown();

	    System.out.print(mf.rightIndent(td.format(money), 17, ' ')+" ");
	    System.out.print(mf.rightIndent(td.format(100*money_up/money),8,' ')+"% ");
	    System.out.print(mf.rightIndent(td.format(100*money_down/money),8,' ')+"%");

	    System.out.print(mf.rightIndent(naz.getNazChange(date),8,' '));

	    net_money += (money_down + money_up);
	    System.out.println(mf.rightIndent(td.format(net_money),15,' ')+" ");
	    //industry_weight = (100*money/totalMoney +100*stock/totalStock+100*volume/totalVolume)/3;


	    temp = industry_name;
	}

	MyPrint.printLine("=",130);
    }

}








